/*
 *     WARNING!
 *  In this class, Z is treated as the vertical axis
 *  This defies the minecraft standard of Y being the vertical axis
 *  
 *  Also, Minecraft treats south and west as positive, which is reversed here.
 *  I blame Notch for having a weird mind
 *  
 *  It is highly recommended that the ONLY static directions be used
 *  If a composite direction is needed, such as North-East, use merge()
 *  
 *  Axis  |  Direction (this class)  |  Direction (Minecraft)
 *    X   |   East (+) / West (-)    |   South (+) / North (-)
 *    Y   |   North (+) / South (-)  |   Up (+) / Down (+)
 *    Z   |   Up (+) / Down (-)      |   West (+) / East (-)
 */
public class CardinalDirection {
	public static CardinalDirection NORTH = new CardinalDirection(0,1,0);
	public static CardinalDirection SOUTH = new CardinalDirection(0,-1,0);
	public static CardinalDirection EAST = new CardinalDirection(1,0,0);
	public static CardinalDirection WEST = new CardinalDirection(-1,0,0);
	public static CardinalDirection UP = new CardinalDirection(0,0,1);
	public static CardinalDirection DOWN = new CardinalDirection(0,0,-1);
	public static int X = 0;
	public static int Y = 1;
	public static int Z = 2;
	public static int CLOCKWISE = -1;
	public static int COUNTERCLOCKWISE = 1;
	private int[] direction = new int[3];
	
	public CardinalDirection(int x, int y, int z) {
		direction[X] = x/Math.abs(x);
		direction[Y] = y/Math.abs(y);
		direction[Z] = z/Math.abs(z);
	}
	public CardinalDirection(int[] d) {
		direction = d;
	}
	public int[] getDirection() { return direction; }
	public int getX() { return direction[X]; }
	public int getY() { return direction[Y]; }
	public int getZ() { return direction[Z]; }
	public CardinalDirection merge(CardinalDirection d) {
		return new CardinalDirection(direction[X]+d.getX(),direction[Y]+d.getY(),direction[Z]+d.getZ());
	}
	
	public void setReverse() {
		direction[X] = -direction[X];
		direction[Y] = -direction[Y];
		direction[Z] = -direction[Z];
	}
	public void setReverse(int axis) {
		direction[axis] = -direction[axis];
	}
	public void setRotate(int axis, int rotation) {
		
	}

	public CardinalDirection reverse() {
		return new CardinalDirection(-direction[X],-direction[Y],-direction[Z]);
	}
	public CardinalDirection reverse(int axis) {
		int[] temp = direction;
		temp[axis] = -temp[axis];
		return new CardinalDirection(temp);
	}

	public SimpleLocation toVector() {
		int x,y,z;
		x = -direction[Y];
		y =  direction[Z];
		z = -direction[X];
		return new SimpleLocation(x,y,z);
	}
	
	public static CardinalDirection fromButton(int data) {
		CardinalDirection d;
		if((data & 0x1) > 0) {
			d = SOUTH;
		} else if((data & 0x2) > 0) {
			d = NORTH;
		} else if((data & 0x3) > 0) {
			d = WEST;
		} else if((data & 0x4) > 0) {
			d = EAST;
		} else {
			return null;
		}
		return d;
	}
}
